package com.zzq.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzq.domain.dto.SmileFeeDTO;
import com.zzq.domain.entity.*;
import com.zzq.enumerate.OssPathEnum;
import com.zzq.enumerate.ShootTypeEnum;
import com.zzq.result.SmileResult;
import com.zzq.domain.dto.SmileShootAddressDTO;
import com.zzq.domain.search.ShootAddressSearchParamet;
import com.zzq.enumerate.AuditEnum;
import com.zzq.security.util.UserUtil;
import com.zzq.service.*;
import com.zzq.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Service
public class ShootAdressServiceImpl implements ShootAddressService {

    @Autowired
    SmileShootAddressService smileShootAddressService;

    @Autowired
    SmileShootAddressApplyService smileShootAddressApplyService;

    @Autowired
    SmileFeeService smileFeeService;

    @Autowired
    SmileShootAddressSampleService smileShootAddressSampleService;

    @Autowired
    OssFileUtil ossFileUtil;

    @Override
    public SmileResult list(ShootAddressSearchParamet searchParamet) {
        LambdaQueryWrapper<SmileShootAddress> wrapper=new LambdaQueryWrapper();
        List<SmileShootAddressDTO> shootAddressDTOs = smileShootAddressService.getShootAddressDTO(searchParamet);
        if (ObjectUtils.isEmpty(shootAddressDTOs)) {
            return SmileResult.waring("暂无数据");
        }
        return SmileResult.sucess("查询成功",shootAddressDTOs);
    }

    @Override
    @Transactional
    public SmileResult add(SmileShootAddressDTO shootAddress) {
        SmileFee smileFee = shootAddress.getSmileFee();
        SmilePhotographer user = UserUtil.getUser();
        shootAddress.setPhotographerId(user.getId());
        shootAddress.setPhotographerAccount(user.getPhotographerAccount());
        SmileShootAddress smileShootAddress = BeanCopyUtils.copyBean(shootAddress, SmileShootAddress.class);


        //暂时设置审核通过
        Boolean scopeTarget = smileShootAddressService.getScopeTarget(shootAddress.getShootLongitude(), shootAddress.getShootLatitude(), 2000L);
        if (scopeTarget){
            smileShootAddress.setAudit(AuditEnum.AUDITING.getName());
        }else {
            smileShootAddress.setAudit(AuditEnum.AUDITED.getName());
        }

        String shootAddressDescription = "";
        String feeType = smileFee.getFeeType();

        //自动编辑拍摄描述
        if (feeType.equals(ShootTypeEnum.FEEFIRST.getName())){
            shootAddressDescription= ShootTypeEnum.FEEFIRST.getDescribe()+
                    "（前 "+smileFee.getFeeShootMax()+
                    " 张 "+smileFee.getFeeAmount()+
                    " 元，超出部分按一张 "+
                    smileFee.getFeeAmountFull()+" 元）";
        }
        else if (feeType.equals(ShootTypeEnum.FEESECOND.getName())){
            shootAddressDescription= ShootTypeEnum.FEESECOND.getDescribe()+
                    "（一套 "+smileFee.getFeeAmount()+" 元）";
        }
        else if (feeType.equals(ShootTypeEnum.FEETHIRD.getName())){
            shootAddressDescription= ShootTypeEnum.FEETHIRD.getDescribe()+
                    "（前 "+smileFee.getFeeShootMax()+
                    " 张 "+smileFee.getFeeAmount()+
                    " 元，超出部分按一张 "+
                    smileFee.getFeeAmountFull()+
                    " 元，当前相册购买数量超过 "+
                    smileFee.getFeeShootFull()+
                    " 张时 ，赠送照片 "+
                    smileFee.getFeeAnnexationNum()+
                    " 张）";
        }

        smileShootAddress.setShootAddressDescription(shootAddressDescription);
        smileShootAddressService.save(smileShootAddress);
        Long shootAddressId = smileShootAddress.getId();
        smileFee.setShootAddressId(shootAddressId);
        smileFeeService.save(smileFee);

        return SmileResult.sucess("上传成功",smileShootAddress);
    }

    @Override
    @Transactional
    public SmileResult addAddressSample(Long shootAddressId, MultipartFile file) throws IOException, ClientException {
        SmileShootAddressSample smileShootAddressSample = new SmileShootAddressSample();
        smileShootAddressSample.setShootAddressId(shootAddressId);
        String dir = ossFileUtil.uploadStaticFile(file, OssPathEnum.PHOTOGRAPHER_ADDRESS_SAMPLE.getPath(), String.valueOf(shootAddressId));
        String staticFileUrl = ossFileUtil.getStaticFileUrl(dir);
        smileShootAddressSample.setShootAddressSamplePhotoUrl(staticFileUrl);
        smileShootAddressSampleService.save(smileShootAddressSample);
        return SmileResult.sucess("上传成功");
    }

    @Override
    @Transactional
    public SmileResult delAddressSample(Long shootAddressSampleId) {
        smileShootAddressSampleService.removeById(shootAddressSampleId);
        return SmileResult.sucess("删除成功");
}

    @Override
    public SmileResult feeUpd(SmileFeeDTO smileFeeDTO) {
        SmileFee smileFee = BeanCopyUtils.copyBean(smileFeeDTO, SmileFee.class);
        smileFeeService.updateById(smileFee);
        return SmileResult.sucess("删除成功");
    }

    @Override
    public SmileResult addPhoto(Long shootAddressId,MultipartFile file) throws IOException, ClientException {
        Long userID = UserUtil.getUserID();
        String s = ossFileUtil.uploadStaticFile(file, OssPathEnum.Address_RESOURCES.getPath(), userID + "");
        String fileUrl = ossFileUtil.getStaticFileUrl(s);
        LambdaUpdateWrapper<SmileShootAddress> wrapper=new LambdaUpdateWrapper<SmileShootAddress>();
        wrapper.eq(SmileShootAddress::getId,shootAddressId);
        wrapper.set(SmileShootAddress::getShootAddressPhoto,fileUrl);
        smileShootAddressService.update(wrapper);
        return SmileResult.sucess("文件上传成功");
    }

    @Override

    public SmileResult getApply(SmileShootAddressApply apply) {
        Long userID = UserUtil.getUserID();
        LambdaQueryWrapper<SmileShootAddressApply> wrapper=new LambdaQueryWrapper();
        wrapper.eq(!ObjectUtils.isEmpty(apply.getShootAddressId()),
                        SmileShootAddressApply::getShootAddressId,
                        apply.getShootAddressId())
                .eq(!ObjectUtils.isEmpty(userID),
                        SmileShootAddressApply::getPhotographerId,
                        userID);
        SmileShootAddressApply addressApply = smileShootAddressApplyService.getOne(wrapper);
        if (!ObjectUtils.isEmpty(addressApply)){
            return SmileResult.sucess("已申请地址",addressApply);

        }else {
            addressApply=new SmileShootAddressApply();
            addressApply.setAudit(AuditEnum.UNAUDIT.getName());
            return SmileResult.sucess("未申请地址",addressApply);
        }
    }

    @Override
    @Transactional
    public SmileResult addApply(SmileShootAddressApply apply) {
        Long userID = UserUtil.getUserID();
        apply.setPhotographerId(userID);
        apply.setAudit(AuditEnum.AUDITING.getName());
        smileShootAddressApplyService.save(apply);
        return SmileResult.sucess("已提交审核",apply);
    }

    @Override
    public SmileResult getQRCode(ShootAddressSearchParamet searchParamet) throws Exception {
        Long userID = UserUtil.getUserID();
        Long shootAddressId = searchParamet.getShootAddressId();
        String url="https://www.secondpretty.com?shoot_address_id="+shootAddressId+"&"+"photographer_id="+userID;
        String rQjson = QRCodeUtil.getRQjson(url);
        try {
        } catch (Exception e) {
            return SmileResult.error("二维码生成失败");
        }
        return SmileResult.sucess("获取成功",rQjson);
    }
}
